var ds;
function displaySearchConfig() {
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.QuickTips.init();
	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	ds = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'scs',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'description'
			}, {
				name : 'boost'
			}, {
				name : 'creator'
			}, {
				name : 'updateTime'
			} ]
		})
	});

	var gridCm = new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), {
		header : TXT_search_config_name,
		dataIndex : 'name',
		width : 150
	}, {
		header : TXT_common_description,
		dataIndex : 'description',
		width : 180
	}, {
		header : TXT_search_config_boost,
		dataIndex : 'boost',
		width : 200
	}, {
		header : TXT_search_config_creator,
		dataIndex : 'creator',
		width : 185
	}, {
		header : TXT_search_config_time,
		dataIndex : 'updateTime',
		width : 200
	} ]);

	var btnCreate = new Ext.Button({
		text : TXT_common_create,
		tooltip : TXT_common_create,
		iconCls : 'icoAdd'
	});
	var btnEdit = new Ext.Button({
		text : TXT_common_edit,
		tooltip : TXT_common_edit,
		iconCls : 'icoDetail'
	});
	var btnDelete = new Ext.Button({
		text : TXT_common_delete,
		tooltip : TXT_common_delete,
		iconCls : 'icoDel'
	});
	var siteDs = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'sites',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'code'
			} ]
		})
	});
	var siteCombo = new Ext.form.ComboBox({
		id : 'siteCb',
		name : 'siteCb',
		mode : 'local',
		forceSelection : false,
		displayField : 'name',
		valueField : 'code',
		emptyText : '--切换驻地--',
		width : 90,
		triggerAction : 'all',
		selectOnFocus : true,
		triggerClass : 'x-form-trigger2',
		store : siteDs,
		lazyInit : false,
		editable : false
	});
	siteCombo.on('select', function(combo, record, index) {
		ds.load({
			params : {
				start : 0,
				limit : PAGE_SIZE,
				siteCode : record.get('code')
			}
		});
		BestvSearch.sc = record.get('code');
	});
	siteDs.baseParams = {
		cmd : 'site',
		action : 'find'
	};
	btnCreate.on('click', function() {
		createSearchFieldInfoForm('');
	});
	btnEdit.on('click', function() {
		if (sm.getCount() == 0) {
			Ext.MessageBox.alert(TXT_common_hint, TXT_user_need);
			return;
		}
		var record = sm.getSelected();
		createSearchFieldInfoForm(record.id);
	});

	btnDelete
			.on(
					'click',
					function() {
						if (sm.getCount() == 0) {
							Ext.MessageBox
									.alert(TXT_common_hint, TXT_user_need);
							return;
						}
						var record = sm.getSelected();
						Ext.MessageBox
								.confirm(
										TXT_common_hint,
										TXT_user_delete_confirmation,
										function(btn) {
											if (btn == 'yes') {
												BestvSearch.showWaitingWin();
												Ext.Ajax
														.request({
															url : DISPATCH_URL,
															method : 'POST',
															params : 'cmd=searchConfig&action=delete&id='
																	+ record.id
																	+ '&sc='
																	+ BestvSearch.sc,
															success : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																var s = response.responseText;
																if (s == '[error]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_access_fail);
																} else if (s == '[null]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_relogin,
																					function() {
																						window.location.href = LOGIN_URL;
																					});
																} else {
																	var result = Ext.util.JSON
																			.decode(s);
																	if (result.result == 'success') {
																		reloadSearchConfig(BestvSearch.sc);
																	} else if (result.message == 'noright') {
																		Ext.MessageBox
																				.alert(
																						TXT_common_hint,
																						TXT_delete_subtype_no_right);
																		return;
																	}
																}
															},
															failure : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_common_access_fail);
															}
														});
											}
										});
					});
	var grid = new Ext.grid.GridPanel({
		el : 'grid',
		ds : ds,
		cm : gridCm,
		sm : sm,
		stripeRows : true,
		loadMask : true,
		title : TXT_sc_title,
		viewConfig : {
			forceFit : true
		},
		tbar : [ btnCreate, '-', btnEdit, '-', btnDelete, '->', '切换驻地: ',
				siteCombo, '-' ],
		bbar : new Ext.PagingToolbar({
			pageSize : PAGE_SIZE,
			store : ds,
			displayInfo : true,
			displayMsg : TXT_common_paging,
			emptyMsg : TXT_common_no
		})
	});
	ds.baseParams = {
		cmd : 'searchConfig',
		action : 'find'
	};
	ds.on('load', function() {
		grid.getSelectionModel().selectFirstRow();
	});
	BestvSearch.glb_mainPanel.add(grid);
	BestvSearch.drawMainFrame(TXT_app_name + ' - ' + TXT_sc_title, siteDs);
	siteDs.on('load', function() {
		ds.load({
			params : {
				start : 0,
				limit : PAGE_SIZE,
				siteCode : BestvSearch.sc
			}
		});
		siteCombo.setValue(BestvSearch.sc);
		siteCombo.setDisabled(true);
	});
	// siteDs.load();
}
function reloadSearchConfig(sc) {
	ds.load({
		params : {
			start : 0,
			limit : PAGE_SIZE,
			siteCode : sc
		}
	});
}
function createSearchFieldInfoForm(id) {
	var sc = BestvSearch.sc;
	var sid = id;
	var searchConfigForm = new Ext.FormPanel({
		labelAlign : 'right',
		labelWidth : 125,
		autoHeigth : true,
		width : 400,
		margins : '0 0 2 0',
		cmargins : '0 0 2 0',
		frame : true,
		defaults : {
			width : 220
		},
		defaultType : 'textfield',
		items : [
				{
					fieldLabel : '<b style="color:red;">* </b> 搜索域名',
					id : 'sname',
					name : 'sname',
					allowBlank : false,
					disabled : false
				},
				new Ext.form.NumberField({
					fieldLabel : '<b style="color:red;">* </b> 搜索权重',
					emptyText : '1-10之间的2位小数',
					id : 'sweight',
					name : 'sweight',
					allowDecimals : true, // 允许小数点
					allowNegative : false, // 允许负数
					allowBlank : false,
					listeners : {
						'render' : {
							fn : function() {
								Ext.getCmp('sweight').getEl().on(
										'keyup',
										function(field, e) {
											var v = Ext.getCmp('sweight')
													.getValue();
											if (v > 10) {
												Ext.Msg.alert(TXT_common_hint,
														'请输入1-10之间的数字');
												Ext.getCmp('sweight').setValue(
														'');
											}
										}, this, {
											buffer : 10
										});
							}
						}
					}
				}), {
					fieldLabel : '<b style="color:red;">* </b>搜索域描述',
					id : 'sdescription',
					name : 'sdescription',
					allowBlank : false
				} ]
	});
	var title;
	if (sid == '')
		title = TXT_common_create;
	else
		title = TXT_common_edit;

	var win = null;
	win = new Ext.Window(
			{
				title : title,
				autoHeigth : true,
				width : 400,
				autoScroll : false,
				layout : 'form',
				border : false,
				minimizable : false,
				maximizable : false,
				resizable : false,
				modal : true,
				layoutConfig : {
					animate : false
				},
				items : searchConfigForm,
				buttonAlign : 'center',
				buttons : [
						{
							text : TXT_common_OK,
							handler : function(response, options) {
								if (!searchConfigForm.getForm().isValid())
									return;
								var sname = searchConfigForm.findById('sname')
										.getValue();
								var sdescription = searchConfigForm.findById(
										'sdescription').getValue();
								var sweight = searchConfigForm.findById(
										'sweight').getValue();
								BestvSearch.showWaitingWin();
								Ext.Ajax
										.request({
											url : DISPATCH_URL,
											method : 'post',
											params : 'cmd=searchConfig&action=save&id='
													+ sid
													+ '&nm='
													+ sname
													+ '&dp='
													+ sdescription
													+ '&wt='
													+ sweight
													+ '&sc='
													+ sc,
											success : function(response,
													options) {
												Ext.MessageBox.hide();
												var s = response.responseText;
												if (s == 'error') {
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_access_fail);
												} else if (s == 'null') {
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_relogin,
																	function() {
																		window.location.href = '../eni/common/logout.jsp';
																	});
												} else {
													var result = Ext.util.JSON
															.decode(s);
													if (result.message == 'fieldNumm') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_null);
														return;
													}
													if (result.result == 'success') {
														reloadSearchConfig(sc);
														win.close();
													}
												}
											},
											failure : function(response,
													options) {
												Ext.MessageBox.hide();
												Ext.MessageBox.alert(
														TXT_common_hint,
														TXT_common_access_fail);
											}
										});
							}
						}, {
							text : TXT_common_cancel,
							handler : function() {
								win.close();
							}
						} ]
			});
	win.on('show', function() {
		if (sid != '') {
			Ext.Ajax.request({
				url : DISPATCH_URL,
				method : 'post',
				params : 'cmd=searchConfig&action=get&id=' + sid + '&sc=' + sc,
				success : function(response, options) {
					var s = response.responseText;
					if (s == '[error]')
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_access_fail);
					else if (s == '[null]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_relogin, function() {
									window.location.href = LOGIN_URL;
								});
					} else if (s == '[existed]') {
						Ext.MessageBox.alert(TXT_common_hint, '相同搜索域已经存在');
					} else {
						var result = Ext.util.JSON.decode(s);
						searchConfigForm.findById('sname').setValue(result.nm);
						searchConfigForm.findById('sname').setDisabled(true);
						searchConfigForm.findById('sdescription').setValue(
								result.dp);
						searchConfigForm.findById('sweight')
								.setValue(result.bt);
					}
				},
				failure : function(response, options) {
					Ext.MessageBox.alert(TXT_common_hint,
							TXT_common_access_fail);
				}
			});
		}
	});
	win.show();
}
Ext.onReady(displaySearchConfig);
